//app.js
var network = require("./utils/request.js")

App({
  onLaunch: function () {
    //提示更新需要1.9.0版本
    if (wx.getUpdateManager) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate)
      })
      updateManager.onUpdateReady(function () {
        wx.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success: function (res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        })
      })
      updateManager.onUpdateFailed(function () {
        wx.showModal({
          title: '更新失败！',
          content: '请手动删除小程序后重新打开。',
        })
      })
    }
    else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '使用更多小程序功能请在我的设置中更新微信版本后打开！',
        success: function (res) {
          if (res.confirm) {

          }
        }
      })
    }
  },
  //保存用户信息
  saveMineMess: function (e) {
    var that = this;
    that.globalData.nickName = e.userInfo.nickName;
    return new Promise(function (resolve, reject) {
    network.requestLoading('POST', that.globalData.requestUrl + 'addConsumerInfoAPI',
      {
        openId: that.globalData.openid,
        nickName: e.userInfo.nickName,
        sex: e.userInfo.gender,
        city: e.userInfo.city,
        head: e.userInfo.avatarUrl,
      }, '', function (res) {
        that.globalData.consumerid = res.data.id;
        that.globalData.userInfo = res.data
        resolve(res);
      })
    })
  },

  //获取openid
  getOpenId: function () {
    var that = this;
    return new Promise(function (resolve, reject) {
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            network.requestLoading('POST', that.globalData.requestUrl + '/userLoginAPI', { code: res.code }, '', function (res) {
              
              that.globalData.openid = res.openid
              that.globalData.userInfo = res.data
              resolve(res);
            }, function (res) {
            
            });
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      })
    })
  },
 
  globalData: {
    requestUrl: 'https://www.thinda.xyz/yitong/phone/',
    consumerid: '',
    uploadUrl: 'https://www.thinda.xyz/yitong/file/upload',
    payUrl: 'https://www.thinda.xyz/yitong/pay/ payWeChat',
    rule: ''
  }
})
